// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from "axios";
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';



axios.interceptors.request.use(function (config) {
  // Do something before request is sent
  //获得token值，如果非空，交给后端进行验证
  var token=localStorage.getItem("token");
  if(token!=null){
    //设置请求头部信息
    config.headers.token=token;
  }
  return config;
}, function (error) {
  // Do something with request error
  return Promise.reject(error);
});

// Add a response interceptor
axios.interceptors.response.use(function (response) {
  // Do something with response data
  // console.log("response interceptor===:",response)
  return response;
}, function (error) {
  // Do something with response error
  return Promise.reject(error);
});



//路由守卫
router.beforeEach((to, from, next) => {
  //console.log(to,from)
  var token=localStorage.getItem("token");
  var isAuthenticated=false;
  if(token){
    isAuthenticated=true;
  }
  if(to.name!=='login' && !isAuthenticated){
    if(to.name=='index') next()
    else next({name:'login'})
  }else next()
})


Vue.use(ElementUI);
Vue.prototype.$axios=axios;
Vue.config.productionTip = false
axios.defaults.baseURL='http://localhost:8081/'

/* eslint-disable no-new */
new Vue({
  el: '#app',
  render: h => h(App),
  router,
  components: { App },
  template: '<App/>'
})
